import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import {
  LoginUserVO,
  UserControllerService,
  UserRegisterRequest,
} from "../../generated";
import userRole from "@/access/permissionEnum";

export const useUser = defineStore(
  "user",
  () => {
    const loginUser = ref({
      createTime: "",
      id: 12,
      updateTime: "",
      userAccount: "",
      userAvatar: "",
      userName: "",
      userProfile: "",
      userRole: userRole.NOT_LOGIN,
    } as LoginUserVO);

    const registerUser = reactive({
      userAccount: "",
      userAvatar: "",
      userName: "",
      userProfile: "",
    } as UserRegisterRequest);

    const defaultRoutes = ref([""]);

    const stateRegister = ref(0);

    const registerRoutes = ref([
      "/register",
      "/register/userInfo",
      "/register/userOther",
    ]);

    const nextPro = () => {
      stateRegister.value += 0.3;
    };

    async function getLoginUser() {
      console.log("获取登录的对象");
      const userResponse = await UserControllerService.getLoginUserUsingGet();
      if (userResponse.code == 0) {
        if (userResponse.data != null) {
          loginUser.value = userResponse.data;
        } else {
          loginUser.value = {
            createTime: "",
            id: 12,
            updateTime: "",
            userAccount: "",
            userAvatar: "",
            userName: "",
            userProfile: "",
            userRole: userRole.NOT_LOGIN,
          } as LoginUserVO;
        }
      }
    }

    return {
      loginUser,
      stateRegister,
      registerUser,
      getLoginUser,
      defaultRoutes,
      nextPro,
      registerRoutes,
    };
  },
  {
    persist: {
      enabled: true,
    },
  }
);
